use std::collections::HashMap;

fn get_query_string() -> Option<String> {
    let query = &web_sys::window()?.location().search().ok()?;
    if !query.is_empty() {
        let query_string = unsafe { query.get_unchecked(1..query.len()).to_string() };

        return Some(query_string);
    }
    None
}

fn split_params(query: &str) -> Vec<String> {
    query.split('&').map(|x| x.to_string()).collect()
}

fn build_params(query_string: &str) -> HashMap<String, String> {
    let mut query_set: HashMap<String, String> = std::collections::HashMap::new();
    for param in split_params(query_string) {
        let items: Vec<&str> = param.split('=').collect();
        query_set.insert(items[0].to_string(), items[1].to_string());
    }
    query_set
}

pub fn get_query_param(id: &str) -> Option<String> {
    build_params(&get_query_string()?).get(id).cloned()
}
